﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace MS.InformationManagement.WP.DocumentCenter.AdminPanel
{
    [ToolboxItemAttribute(false)]
    public class AdminPanel : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/MS.InformationManagement.WP.DocumentCenter/AdminPanel/AdminPanelUserControl.ascx";

        public string ListName { get { return Page.Request.QueryString["list"]; } }

        protected override void CreateChildControls()
        {
            Control control = Page.LoadControl(_ascxPath); 
            Controls.Add(control);
            if (!string.IsNullOrEmpty(ListName))
                loadWebparts();
        }

        private void loadWebparts()
        {
            SPList currentList = SPContext.Current.Web.Lists[ListName];
            SPView view = currentList.DefaultView;
            XsltListViewWebPart _Lvwp = new XsltListViewWebPart();
            _Lvwp.ListName = currentList.ID.ToString("B").ToUpper();
            _Lvwp.ViewGuid = view.ID.ToString("B").ToUpper();
            _Lvwp.SuppressWebPartChrome = true;
            _Lvwp.ZoneID = "Zone 1";
            Controls.Add(_Lvwp);
            Title = currentList.Title;
            TitleUrl = view.ServerRelativeUrl;
            string wpcid = SPRibbon.GetWebPartPageComponentId(this);
            string lvwpcid = SPRibbon.GetWebPartPageComponentId(_Lvwp);
            string s = @"
                    <script type=""text/javascript"">
                    Type.registerNamespace('EmbeddedListViewWebPart');
                    var _webPartPageComponentId;
                    var _lvwpPC;
                    EmbeddedListViewWebPart.CustomPageComponent = function EmbeddedListViewWebPart_CustomPageComponent(webPartPcId,lvwpPc){
                        this._webPartPageComponentId = webPartPcId;
                        this._lvwpPC = lvwpPc;
                        EmbeddedListViewWebPart.CustomPageComponent.initializeBase(this);
                    }
                    EmbeddedListViewWebPart.CustomPageComponent.prototype = {
                        init: function EmbeddedListViewWebPart_CustomPageComponent$init() { },
                        getFocusedCommands: function EmbeddedListViewWebPart_CustomPageComponent$getFocusedCommands() { return this._lvwpPC.getFocusedCommands(); },
                        getGlobalCommands: function EmbeddedListViewWebPart_CustomPageComponent$getGlobalCommands() { return this._lvwpPC.getGlobalCommands(); },
                        isFocusable: function EmbeddedListViewWebPart_CustomPageComponent$isFocusable() { return this._lvwpPC.isFocusable(); },
                        canHandleCommand: function EmbeddedListViewWebPart_CustomPageComponent$canHandleCommand() { return this._lvwpPC.canHandleCommand(); },
                        handleCommand: function EmbeddedListViewWebPart_CustomPageComponent$handleCommand() { return this._lvwpPC.handleCommand(); },
                        getId: function EmbeddedListViewWebPart_CustomPageComponent$getId() { return this._webPartPageComponentId; },
                        yieldFocus: function EmbeddedListViewWebPart_CustomPageComponent$yieldFocus() { return this._lvwpPC.yieldFocus(); },
                        receiveFocus: function EmbeddedListViewWebPart_CustomPageComponent$receiveFocus() { return this._lvwpPC.receiveFocus(); }
                    }
                    function fixEmbeddedListView() {
                        alert('eje');
                        EmbeddedListViewWebPart.CustomPageComponent.registerClass('EmbeddedListViewWebPart.CustomPageComponent', CUI.Page.PageComponent);
                        var pageMan = SP.Ribbon.PageManager.get_instance();
                        var pageComponent = pageMan.getPageComponentById('" + lvwpcid + @"');
                        var _cpc = new EmbeddedListViewWebPart.CustomPageComponent('" + wpcid + @"',pageComponent);
                        pageMan.addPageComponent(_cpc);
                    }
                    ExecuteOrDelayUntilScriptLoaded(fixEmbeddedListView, 'sp.ribbon.js');
                    alert('aja');
                    </script>";
            Controls.Add(new LiteralControl(s));
        }
    }
}
